$(function () {
    let userId = "";
    let windowH = $(window).height() - 18;
    $("#wrapper").attr('style','height: ' + windowH + 'px');
    let showMenuList = [];
    isLogin();
    $("#sureLogin").click(function () {
        let account = $("#account").val()
        let password = $("#password").val();
        toLogin(account,password);
    });
    function toLogin(account,password) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/user/login",
            data: {
                condition:account,
                password:password
            },
            success: function (result) {
                if (result.returnCode == "0000"){
                    $("#login").attr('style','top:10000px;');
                    let user = result.bean;
                    userId = user.userId;
                    let beans = result.beans;
                    let menuList = result.menuList;
                    let pMenuList = result.pMenuList;
                    showMenus(pMenuList,menuList);

                    str = '<li style="background: #0089ff;" li-menuaction="clock">\n' +
                        '<a class="menu-li-a" name="menu-li-a-clock" style="color: white;" href="javascript:;">' + '首页' + '</a>\n' +
                        '</li>';
                    $(".nav-list").append(str);
                    let iframe = '<iframe class="iframe" src="/getNav?menuAction=' + "clock" + '" name="' + "iframe-" + "clock" + '" frameborder="0" width="100%" scrolling="no"  height="100%"></iframe>'
                    showMenuList.push("clock");
                    $(".content").append(iframe);
                }else {
                    alert(result.returnMsg);
                }
            },
            error : function() {
                alert("异常！");
            }
        });
    }

    $("#sureRegister").click(function () {
        let nickName = $("#nick-name").val();
        let password = $("#register-password").val();
        let phoneNumber = $("#phone-number").val();
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/user/register",
            data: {
                nickname:nickName,
                password:password,
                phonenumber:phoneNumber
            },
            success: function (result) {
                if (result.returnCode == "0000"){
                    alert("注册成功，您的账号是" + result.bean.userCode + " ,请登录")
                }else {
                    alert(result.returnMsg);
                }
            },
            error : function() {
                alert("异常！");
            }
        });
    });
    let pMenus = ["sys-list","busi-list"];
    $("#sys-setting").click(function () {
        if ($("#sys-list").is(".xiaoshi")){
            $("#sys-list").removeClass("xiaoshi")
            $("#busi-list").addClass("xiaoshi")
        }else {
            $("#sys-list").addClass("xiaoshi")
        }
    })

    $("#busi-setting").click(function () {
        if ($("#busi-list").is(".xiaoshi")){
            $("#busi-list").removeClass("xiaoshi")
            $("#sys-list").addClass("xiaoshi")
        }else {
            $("#busi-list").addClass("xiaoshi")
        }
    })

    function isLogin() {
        let account = $("#account").val()
        let password = $("#password").val();
        if (account.length > 0 && password.length > 0){
            toLogin(account,password)
        }else {
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "/islogin",
                success: function (result) {
                    let islogin = result.islogin;
                    if ("0" == islogin){
                        $("#login").attr('style','top:0px;')
                    }
                },
                error : function() {
                    alert("异常！");
                }
            });
        }
    }

    function showMenus(pMenuList,menuList) {
        let str = "";
        let menuId = "";
        for (let i = 0; i < pMenuList.length; i ++){
            menuId = pMenuList[i].menuId;
            str = '<div class="menu-list">' +
                     '<div class="parent-menu" name="guan" id="' + pMenuList[i].menuEnName + '">' + pMenuList[i].menuName + '</div>';
             for (let j = 0; j < menuList.length; j ++){
                 if (j == 0){
                     str = str + '<ul class="xiaoshi" name="aaa">'
                 }
                 if (menuList[j].parentId == menuId){
                     str = str + '<li menuaction="' + menuList[j].menuAction + '" menuname="' + menuList[j].menuName + '"><a class="change-menu" href="javascript:;">' + menuList[j].menuName + '</a></li>';
                 }
             }
             str = str + '</ul>' +
                            '</div>';
             $("#left_nav").append(str);
             str = "";
        }

        //绑定点击事件
        $(document).on('click','.parent-menu',function (item) {
            let doms = document.getElementsByName("aaa");
            let dom = $(this).parent().find('ul');
            let thisDomKaiGuan = $(this).attr("name");
            let thisClass = $(this).attr("class");
            for (let i = 0; i < doms.length; i++){
                $(doms[i]).attr("class","xiaoshi");
                $(doms[i]).parent().find("div").attr("name","guan");
            }
            $(this).attr("name",thisDomKaiGuan);
            $(this).attr("class",thisClass);

            if ($(this).attr("name") == "kai"){
                dom.attr("class","xiaoshi");
                $(this).attr("name","guan");
            }else if($(this).attr("name") == "guan"){
                dom.attr("class","");
                $(this).attr("name","kai");
            }
        });

        $(document).on('click','.change-menu',function (item) {
            let menuAction = $(this).parent().attr("menuaction");
            let menuName = $(this).parent().attr("menuname");
            let str = "";
            let isShowing = isShowingMenu(menuAction);
            if (!isShowing){
                let menu_li_a_list = document.getElementsByClassName("menu-li-a");
                if (menu_li_a_list.length > 0){
                    for (let a = 0; a <= menu_li_a_list.length; a ++){
                        $(menu_li_a_list[a]).attr('style','color: black;')
                        $(menu_li_a_list[a]).parent().attr('style','background: #0bc2f8;');
                    }
                }
                str = '<li style="background: #0089ff;" li-menuaction="' + menuAction + '">\n' +
                    '<a class="menu-li-a" name="' + 'menu-li-a-' + menuAction + '" style="color: white;" href="javascript:;">' + menuName + '</a>\n' +
                    '<i class="exit">x</i>\n' +
                    '</li>';
                $(".nav-list").append(str);

                let iframes = document.getElementsByClassName("iframe");
                if (iframes.length > 0){
                    for (let i = 0; i <= iframes.length; i ++){
                        $(iframes[i]).attr("style","display:none;")
                    }
                }
                let iframe = '<iframe class="iframe" src="/getNav?menuAction=' + menuAction + '" name="' + "iframe-" + menuAction + '" frameborder="0" width="100%" scrolling="no"  height="100%"></iframe>'
                //showMenuList用于退出一个菜单后显示前一个菜单
                showMenuList.push(menuAction);
                $(".content").append(iframe);
                recordClick(menuAction)
            }
        });

        /*记录点击*/
        function recordClick(menuAction) {
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "menu/recordClick",
                data: {
                  menuAction:menuAction,
                    userId:userId
                }
            });
        }

        function isShowingMenu(menuAction){
            for (let i = 0; i < showMenuList.length; i ++){
                if (menuAction == showMenuList[i]){
                    return true;
                }
            }
            return false;
        }
        $(document).on('click','.exit',function (item) {
            let index = 0;
            let removeIndex = 0;
            let li = $(this).parent();
            let menuAction = li.attr("li-menuaction");
            li.remove();
            let iframeDom = document.getElementsByName("iframe-" + menuAction);
            iframeDom[0].remove();
            // console.log(showMenuList)
            for (let i = 0; i < showMenuList.length; i ++){
                if (menuAction == showMenuList[i]){
                    removeIndex = i;
                    if (i == 0){
                        index = i + 1;
                    }else {
                        index = i - 1;
                    }
                }
            }
            let nextShowMenuAction = "iframe-" + showMenuList[index];
            console.log(nextShowMenuAction)
            let nextShowDom = document.getElementsByName(nextShowMenuAction);
            $(nextShowDom[0]).attr("style","display:block;");

            let nextShowmenuLia = "menu-li-a-" + showMenuList[index];
            console.log(nextShowmenuLia)
            let nextShowmenuLiaDom = document.getElementsByName(nextShowmenuLia);
            $(nextShowmenuLiaDom[0]).attr('style','color: white;');
            $(nextShowmenuLiaDom[0]).parent().attr('style','background: #0089ff;');
            showMenuList.splice(removeIndex,1);
        });

        $(document).on('click','.menu-li-a',function (item) {
            let menu_li_a_list = document.getElementsByClassName("menu-li-a");
            if (menu_li_a_list.length > 0){
                for (let a = 0; a <= menu_li_a_list.length; a ++){
                    $(menu_li_a_list[a]).attr('style','color: black;')
                    $(menu_li_a_list[a]).parent().attr('style','background: #0bc2f8;');
                }
            }
            $(this).attr('style','color: white');
            $(this).parent().attr('style','background: #0089ff;');
            let menuAction = $(this).parent().attr("li-menuaction");
            let nextShowMenuAction = "iframe-" + menuAction;
            let iframes = document.getElementsByClassName("iframe");
            if (iframes.length > 0){
                for (let i = 0; i <= iframes.length; i ++){
                    $(iframes[i]).attr("style","display:none;")
                }
            }
            let iframeDom = document.getElementsByName(nextShowMenuAction)[0];
            $(iframeDom).attr("style","display:block;");
        });
    }




});